//
// Created by Ivor_ on 2022/3/13.
//

#include "incredibleSet.h"

unsigned int iafLog2(unsigned int variable) {
    variable |= variable >> 1;
    variable |= variable >> 2;
    variable |= variable >> 4;
    variable |= variable >> 8;
    variable |= variable >> 16;
    variable -= variable >> 1 & 0x55555555;
    variable = (variable >> 2 & 0x33333333) + (variable & 0x33333333);
    variable = (variable >> 4) + variable & 0x0f0f0f0f;
    variable += variable >> 8;
    variable += variable >> 16;
    return (variable & 0x0000003f) - 1;
}

void* PRIVATEfun0(void *arg) {
    unsigned int *arg_ = (unsigned int*)arg;
    sleep(*arg_);
    printf("%u\n", *arg_);
    return NULL;
}

void iamSlpSort(int numberOfSortElements, unsigned int* sortElements) {
    pthread_t* pt = (pthread_t*)calloc(numberOfSortElements, sizeof(pthread_t));
    for (int i = 0; i < numberOfSortElements; i++) {
        pthread_create(&pt[i], NULL, PRIVATEfun0, &sortElements[i]);
    }
    for (int i = 0; i < numberOfSortElements; i++) {
        pthread_join(pt[i], (void**)NULL);
    }
    free(pt);
}

void iafPI() {
    int aa = 10000, bb = 0, cc = 10000, dd, ee, ff[10001], gg;
    for (; bb - cc; ) ff[bb++] = aa/5;
    for (; dd = 0, gg = cc*2; cc--, printf("%.4d", ee+dd/aa), ee = dd%aa)
        for (bb = cc; dd += ff[bb]*aa, ff[bb] = dd % --gg, dd /= gg--, --bb; dd *= bb);
}
